<?php 
require('./lib/init.php');
$sql = "select * from cat";
$cats = mGetAll($sql);
//var_dump($cats);
if(empty($_POST)){
	require(ROOT.'/view/admin/artadd.html');
}else{
	//检测标题是否为空
	$art['title'] = trim($_POST['title']);
	if(empty($art['title'])){
		error('标题不能为空');
	}

	//检测内容是否为空
	$art['content'] = trim($_POST['content']);
	if(empty($art['content'])){
		error('标题不能为空');
	}

	//检测栏目是否为数字
	$art['cat_id'] = $_POST['cat_id'];
	if(!is_numeric($art['cat_id'])){
		error('栏目不能为数字');
	}

	//获取要插入的文章时间
	$art['pubtime'] = time();
	if($_FILES['pic']['error'] == 0){
		//目录
		$filename = createDir().'/'.randStr(8).getExt($_FILES['pic']['name']);
		move_uploaded_file($_FILES['pic']['tmp_name'],ROOT.$filename);
		$art['pic'] = $filename;
		//加上缩略图
		$art['thumb'] = makeThumb($filename);
	}
	
	//插入内容到art表
	if(mExec('art',$art)){
		$sql = "update cat set num=num+1 where cat_id=$art[cat_id]";
		mQuery($sql);
		succ('文章添加成功!');

	}else{
		error('文章添加失败,失败原因请参考log日志,谢谢!');
	}
}

